package com.lishe.lishe.lishe.MyRecyclerView;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

/**
 * Created by shino on 2017/11/21.
 * recyclerview 滚出外面的动画效果
 */

public class ItemScrollOutAnimation extends SimpleItemAnimator {
    String TAG = ItemScrollOutAnimation.class.getSimpleName();
    private ArrayList<RecyclerView.ViewHolder> mPendingAddHolders =
            new ArrayList<>();
    private ArrayList<RecyclerView.ViewHolder> mPendingRemoveHolders =
            new ArrayList<>();
    private ArrayList<RecyclerView.ViewHolder> mAddAnimtions = new ArrayList<>();
    private ArrayList<RecyclerView.ViewHolder> mRemoveAnimations = new ArrayList<>();

    private ArrayList<MoveInfo> mPendingMoveHolders =
            new ArrayList<>();
    private ArrayList<MoveInfo> mMoveAnimtions = new ArrayList<>();
    private long time;

    public ItemScrollOutAnimation(long time) {
        this.time = time;
    }

    //移除动画
//    这里我们仅仅是向集合中添加一个holder，并且将返回值置为true，表示可以去执行runPendingAnimations。
//    这里需要注意的就是animateAdd方法的第一行代码，我们将view设置不可见，这样做的目的是防止item闪动(出现后才去执行动画)。
    @Override
    public boolean animateRemove(RecyclerView.ViewHolder holder) {
        holder.itemView.setAlpha(1.f);
        mPendingRemoveHolders.add(holder);
        return true;
    }

    //添加动画
    @Override
    public boolean animateAdd(RecyclerView.ViewHolder holder) {
        holder.itemView.setAlpha(1.f);
        mPendingAddHolders.add(holder);
        return true;
    }

    //滚动动画
    @Override
    public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, int toX, int toY) {
        View view = holder.itemView;
        fromY += view.getTranslationY();
        int delta = toY - fromY;
        view.setTranslationY(-delta);
        MoveInfo info = new MoveInfo(holder, fromX, fromY, toX, toY);
        mPendingMoveHolders.add(info);
        return true;
    }

    @Override
    public boolean animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop) {
        return false;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void runPendingAnimations() {
        boolean isRemove = !mPendingRemoveHolders.isEmpty();
        boolean isAdd = !mPendingAddHolders.isEmpty();
        boolean isMove = !mPendingMoveHolders.isEmpty();

        if (!isRemove && !isAdd) return;
        // first remove
        if (isRemove) {
            for (RecyclerView.ViewHolder holder : mPendingRemoveHolders) {
                animateRemoveImpl(holder);
            }
            mPendingRemoveHolders.clear();
        }

        if (isMove) {
            ArrayList<MoveInfo> infos = new ArrayList<>();
            infos.addAll(mPendingMoveHolders);
            mPendingMoveHolders.clear();
            for (MoveInfo info : infos) {
                animateMoveImpl(info);
            }
            infos.clear();
        }
        // last add
        if (isAdd) {
            ArrayList<RecyclerView.ViewHolder> holders = new ArrayList<>();
            holders.addAll(mPendingAddHolders);
            mPendingAddHolders.clear();
            for (RecyclerView.ViewHolder holder : holders) {
                animateAddImpl(holder);
            }
            holders.clear();
        }
    }


    @Override
    public void endAnimation(RecyclerView.ViewHolder item) {


    }

    @Override
    public void endAnimations() {

    }

    //是否在执行动画
    @Override
    public boolean isRunning() {
        return !(mPendingAddHolders.isEmpty()
                && mPendingRemoveHolders.isEmpty()
                && mAddAnimtions.isEmpty()
                && mRemoveAnimations.isEmpty());
    }

    @Override
    public boolean animateDisappearance(@NonNull RecyclerView.ViewHolder viewHolder, @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) {

        return super.animateDisappearance(viewHolder, preLayoutInfo, postLayoutInfo);
    }

    int i = 0;

    //添加的动画
    private void animateAddImpl(final RecyclerView.ViewHolder holder) {

//        mRemoveAnimations.add(holder);
//        final View view = holder.itemView;
//        long translationY = view.getHeight();
//        AnimatorSet animatorSet = new AnimatorSet();
//        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "alpha", 0.f, 1.f);
//        ObjectAnimator animator10 = ObjectAnimator.ofFloat(view, "alpha", 1.f, 0.f);
//        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationY", 0, translationY);
//        ObjectAnimator animator20 = ObjectAnimator.ofFloat(view, "translationY", translationY, 0);
//        animator1.setDuration(0);
//        animator2.setDuration(0);
//        animator10.setDuration(0);
//        animator20.setDuration(1000);
////        animatorSet.play(animator20).with(animator1).after(animator2).after(animator10);
//        animatorSet.play(animator2);
//        animatorSet.play(animator20).after(animator2);
//        animatorSet.addListener(new AnimatorListenerAdapter() {
//            @Override
//            public void onAnimationStart(Animator animation) {
//                dispatchRemoveStarting(holder);
//            }
//
//            @Override
//            public void onAnimationEnd(Animator animation) {
////                view.setBackgroundColor(Color.parseColor("#ffffff"));
//                mRemoveAnimations.remove(holder);
//                dispatchRemoveFinished(holder);
//                if (!isRunning()) dispatchAnimationsFinished();
//            }
//        });
//        animatorSet.start();
    }

    // 执行移出(除)动画(view会被复用)
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void animateRemoveImpl(final RecyclerView.ViewHolder holder) {
        mRemoveAnimations.add(holder);
        final View view = holder.itemView;
        AnimatorSet animatorSet = new AnimatorSet();
        long translationY = view.getHeight();
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "translationY", 0, -translationY);
        ObjectAnimator animator10 = ObjectAnimator.ofFloat(view, "translationY", -translationY, 0);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "alpha", 1.f, 0.f);
        animator1.setDuration(1000);
        animator10.setDuration(0);
        animator2.setDuration(0);
        animatorSet.play(animator10).with(animator2).after(animator1);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                dispatchRemoveStarting(holder);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mRemoveAnimations.remove(holder);
                view.setAlpha(1.f);
                dispatchRemoveFinished(holder);
                if (!isRunning()) dispatchAnimationsFinished();
            }
        });
        animatorSet.start();
    }

    // 执行移动动画
    private void animateMoveImpl(final MoveInfo info) {
        mMoveAnimtions.add(info);
        final View view = info.holder.itemView;
        view.setVisibility(View.VISIBLE);
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "alpha", 1.f, 0.f);
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,
                "translationY", view.getTranslationY(), 0);
        animator.setDuration(1000);
        animator2.setDuration(1000);
        animatorSet.play(animator).with(animator2);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                dispatchMoveStarting(info.holder);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.setAlpha(1.f);
                dispatchMoveFinished(info.holder);
                mMoveAnimtions.remove(info.holder);
                if (!isRunning()) dispatchAnimationsFinished();
            }
        });
        animatorSet.start();
    }
}
